package com.yc.jpyy.losegoods.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.base.util.PageBean;
import com.yc.jpyy.losegoods.dao.LoseGoodsDao;
import com.yc.jpyy.losegoods.service.LoseGoodsService;
import com.yc.jpyy.utils.UtilDate;

@Service
public class LoseGoodsServiceImpl extends BaseService implements LoseGoodsService {
    
	@Autowired
	private LoseGoodsDao LoseGoodsDao;

	

	@Override
	public int addLoseGoodsDao(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = LoseGoodsDao.addLoseGoodsDao(paramsMap);
		return ret;
	}

	@Override
	public int updateLoseGoodsDao(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = LoseGoodsDao.updateLoseGoodsDao(paramsMap);
		return ret;
		
	}

	@Override
	public int delLoseGoodsDao(Map<String, Object> paramsMap) throws DataAccessException {
		int ret = LoseGoodsDao.delLoseGoodsDao(paramsMap);
		return ret;
	}
	
	

	@Override
	public Map<String, Object> findLoseGoodsByID(Map<String, Object> paramsMap) throws DataAccessException {
	     return LoseGoodsDao.findLoseGoodsByID(paramsMap);
	}

	

	@Override
	public PageBean findLoseGoodsDao(Map<String, Object> paramsMap) throws DataAccessException {
		int pagenum = Integer.parseInt(paramsMap.get("pagenum").toString());
		int pagesize = Integer.parseInt(paramsMap.get("pagesize").toString());
		paramsMap.put("pagestart", (pagenum-1) * pagesize);
		String companyid = Optional.ofNullable(paramsMap.get("companyid")).orElse("").toString();
		paramsMap.put("companyid", companyid.equals("")?"":companyid.split(","));
		List<Map<String, Object>> list = LoseGoodsDao.findLoseGoodsDao(paramsMap);
		PageBean pageBean = new PageBean();
		pageBean.setList(list);
		// 每页几条数据，默认20条
		pageBean.setPageSize(pagesize);
		// 得到数据库中的条数
		pageBean.setAllRow(this.findLoseGoodsDaoCount(paramsMap));
		// 当前第几页，默认第一页
		pageBean.setCurrentPage(pagenum);
		// 得到一共几页
		pageBean.setTotalPage(pageBean.getTotalPage());
		// 模仿百度分页，只显示10条数据
		pageBean.paginationTool(pagenum, pageBean.getTotalPage());
		return pageBean;
	}

	@Override
	public int findLoseGoodsDaoCount(Map<String, Object> paramsMap) throws DataAccessException {
		return LoseGoodsDao.findLoseGoodsDaoCount(paramsMap);
	}

	@Override
	public int delforUpdateLoseGoodsDao(Map<String, Object> loseGoodsInfo) throws DataAccessException {
		return LoseGoodsDao.delforUpdateLoseGoodsDao(loseGoodsInfo);
	}

	@Override
	public int addLoseGoodsForWxService(Map<String, Object> paramsMap) throws Exception {
		int retValue = 0;
		int num = 0 ;
		num = LoseGoodsDao.findwxUserCountToBlackDao(paramsMap);
	    if(num > 0){
		    throw new Exception("此用户为黑名单用户");
		}	
	    num = LoseGoodsDao.findwxUserByOne(paramsMap);
	    if(num > 0){
		    throw new Exception("微信端失物招领一天只能有一次");
	    }
	    paramsMap.put("publishtime", UtilDate.getDateFormatter());
	    paramsMap.put("status", 0);
	    paramsMap.put("inputtype", 2);
	    paramsMap.put("ltype", 2);
	    paramsMap.put("isdelete", 0);
	    retValue = LoseGoodsDao.addLoseGoodsDao(paramsMap);
		return retValue;
	}

	
}
